package com.nanrui.mutualinductor.ui.page;

public class Test {

    public static void main(String[] args) {
        String s = "0x01";

       String sss =  bin2hex(s);
       System.out.println(sss);

        int int100 = 100;
        System.out.println(Integer.toHexString(int100));//64
        int int100_ = -100;
        System.out.println(Integer.toHexString(int100_));//ffffff9c
        //16进制转int,使用Integer.parseUnsignedInt(s, 16)
        System.out.println(Integer.parseUnsignedInt("64", 16));
        System.out.println(Integer.parseUnsignedInt("ffffff9c", 16));
    }

    public static String bin2hex(String bin) {
        char[] digital = "0123456789ABCDEF".toCharArray();
        StringBuffer sb = new StringBuffer("");
        byte[] bs = bin.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(digital[bit]);
            bit = bs[i] & 0x0f;
            sb.append(digital[bit]);
        }
        return sb.toString();
    }
}
